package com.whut.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.ChildsystemMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.PositionMapper;
import com.whut.mapper.UserinfoMapper;
import com.whut.mapper.UserpermissionMapper;
import com.whut.model.Userinfo;
import com.whut.model.Userpermission;
import com.whut.service.PersonnelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("personnelService")
public class PersonnelServiceImpl implements PersonnelService {

    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private UserpermissionMapper userpermissionMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private ChildsystemMapper childsystemMapper;

    @Override
    public String addUser(Userinfo userinfo) {
        try {
            userinfoMapper.insert(userinfo);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Userinfo getUserByUserPhone(String userPhone) {
        return userinfoMapper.selectByUserPhone(userPhone);
    }

    @Override
    public Object getUserByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Userinfo> list = userinfoMapper.getAllUserInfo();
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public String deleteUserByUserId(int userId) {
        try {
            int res = userinfoMapper.deleteByPrimaryKey(userId);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public int updatePasswordByUserId(int userid,String newPassword) {
        return userinfoMapper.updatePasswordByPrimaryKey(userid,newPassword);
    }

    @Override
    public int updateUserinfo(Userinfo user) {
        return userinfoMapper.updateUserinfo(user);
    }

    @Override
    public Object getUserByUserName(String userName) {
        PageHelper.startPage(1, 10);
        List<Userinfo> list = userinfoMapper.getUserInfoByUserName(userName);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getPermission(){
        List<Userpermission> list = userpermissionMapper.selectAllPermission();
        return list;
    }

    @Override
    public Object getPersonalPermission(int userId){
        List<Userinfo> list = userinfoMapper.selectPersonalPermission(userId);
        return list;
    }

    @Override
    public int updatePermission(int userId,String permission){
        return userinfoMapper.updatePermissinById(userId,permission);
    }

    @Override
    public int resetUserPassword(Userinfo user) {
        return userinfoMapper.resetUserPassword(user);
    }

    @Override
    public List<Object> getAllPosition() {
        return positionMapper.getAllPosition();
    }

    @Override
    public List<Object> getAllModular() {
        return userpermissionMapper.getAllModular();
    }

    @Override
    public List<Object> getPermissionByModular(String modular) {
        return userpermissionMapper.getPermissionByModular(modular);
    }

    @Override
    public String setPermissionByPosition(Userpermission userpermission) {
         userpermissionMapper.setPermissionByPosition(userpermission);
         return "success";
    }

    @Override
    public Userpermission getPermissionByPermissionNum(Integer permissionNum) {
        return userpermissionMapper.getPermissionByPermissionNum(permissionNum);
    }

    @Override
    public String deleteByPosition(String position) {
        userpermissionMapper.deleteByPosition(position);
        return "success";
    }

    @Override
    public String deletePosition(String position) {
        positionMapper.deletePosition(position);
        return "success";
    }

    @Override
    public String addPosition(String position) {
        positionMapper.addPosition(position);
        return "success";
    }

    @Override
    public List<Object> getPermissionByPosition(String position) {
        return userpermissionMapper.getPermissionByPosition(position);
    }

    @Override
    public List<Object> getAllChildSystem() {
        return childsystemMapper.getAllChildSystem();
    }

    @Override
    public List<Object> getModularByChildSystem(String childSystem) {
        return childsystemMapper.getModularByChildSystem(childSystem);
    }

    @Override
    public String getPermissionByUserId(String userId) {

        return userinfoMapper.getPermissionByUserId(userId);
    }

    @Override
    public int updatePermissionByPosition(String position,String permission,String newPosition){
        return userinfoMapper.updatePermissionByPosition(position,permission,newPosition);
    }
}
